jQuery(function($) {
	var doc = document,
		doc_ = $(doc);
	
	//输出错误信息
	function sendErrorMessage(node, msg) {
		node.html(msg);
	}
	
	//验证是否为数值
	function checkIsNumber(num) {
		var re = /^[0-9]*$/g;
		return re.test(num);
	}
	
	//校验手机号
	function checkMobile(elm, err) {
		var tel = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/g;

		if (!tel.test(elm.val())) {
			err.show();
			sendErrorMessage(err, "请输入正确的手机号码！");
			elm.css({
				border: "1px solid #f3a3a3"
			});
			done.phone = false;
			return;
		}
		done.phone = true;
	}
	
	//校验邮箱
	function checkEmail(elm, err) {
		var reg = /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/;
		
		if (!reg.test(elm.val())) {
			err.show();
			sendErrorMessage(err, "请输入正确的邮箱！");
			elm.css({
				border: "1px solid #f3a3a3"
			});
			done.email = false;
			return;
		}
		done.email = true;
	}

    var is_finance = 0;
	//显示或隐藏融资表单
	doc_.on("click",  function() {
		var dis = $(".dis"),
			err = $(".c-e-2"),
			err2 = $(".c-e-3");
			
		if ($(".r-need").prop("checked") === true) {
            is_finance = 0;
			dis.hide();
			err.hide().html("");
			err2.hide().html("");
		} else {
            is_finance = 1;
			dis.show();
		}
	});
	
	//验证所有表单
	var done = {
		money: false,
		stock: false,
		realName: false,
		phone: false,
		email: false
	};
	
	//验证融资金额
	//start
	function checkMoney(input) {
		var val = input.val(),
			err = $(".c-e-2");
		
		if (val === "") {
			err.show();
			sendErrorMessage(err, "融资金额不能为空！");
			done.money = false;
			return;
		}
		
		if (!checkIsNumber(val)) {
			err.show();
			sendErrorMessage(err, "融资金额只能为数字！");
			done.money = false;
			return;
		}
		done.money = true;
	}
	
	doc_.on("blur", ".money", function() {
		checkMoney($(this));
	});
	
	doc_.on("focus", ".money", function() {
		err = $(".c-e-2").hide();
	});
	//end
	
	//验证出让股份
	//start
	function checkStock(input) {
		var val = input.val(),
			err = $(".c-e-3");
		
		if (val === "") {
			err.show();
			sendErrorMessage(err, "让出股份不能为空！");
			done.stock = false;
			return;
		}
		
		if (!checkIsNumber(val)) {
			err.show();
			sendErrorMessage(err, "让出股份只能为数字！");
			done.stock = false;
			return;
		}
		
		if (val.length > 100) {
			err.show();
			sendErrorMessage(err, "让出股份不能大于100个字符！");
			done.stock = false;
			return;
		}

		done.stock = true;
	}
	
	doc_.on("blur", ".stock", function() {
		checkStock($(this));
	});
	
	doc_.on("focus", ".stock", function() {
		$(".c-e-3").hide();
	});
	//end
	
	//验证姓名
	//start
	function checkRealName(input) {
		var val = input.val(),
			err = $(".i-e-c");
			
		if (val === "") {
			err.show();
			sendErrorMessage(err, "姓名不能为空！");
			done.realName = false;
			return;
		}
		
		if (val.length > 4) {
			err.show();
			sendErrorMessage(err, "姓名不能大于4个字符！");
			done.realName = false;
			return;
		}
		done.realName = true;
	}
	
	doc_.on("blur", ".real_name", function() {
		checkRealName($(this));
	});
	doc_.on("focus", ".real_name", function() {
		$(".i-e-c").hide();
	});
	//end
	
	
	//验证手机号
	//start
	doc_.on("blur", ".mobile", function() {
		checkMobile($(this), $(".i-e-i"));
	});
	doc_.on("focus", ".mobile", function() {
		$(".i-e-i").hide();
	});
	//end
	
	
	//验证邮箱
	doc_.on("blur", ".email", function() {
		checkEmail($(this), $(".i-e-a"));
	});
	doc_.on("focus", ".email", function() {
		$(".i-e-a").html("");
	});

    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
	
	
	//验证全部表单，通过则提交代码
	doc_.on("click", ".c-next button", function() {
		if ($(".r-need").prop("checked") === true) {
			done.money = true; //如果不需要融资'融资金额'则为true
			done.stock = true; //如果不需要融资'让出股份'则为true
		} else {
			checkMoney($(".money")); //检测融资金额
			checkStock($(".stock")); //检测让出股份
		}
		
		checkRealName($(".real_name"), $(".i-e-c")); //检测姓名
		checkMobile($(".mobile"), $(".i-e-i")); //检测手机号
		checkEmail($(".email"), $(".i-e-a")); //检测邮箱
		
		//如果全部表单通过验证则提交表单
		if (done.money &&
			done.stock &&
			done.realName &&
			done.phone &&
			done.email) {
				
            var money = $('.money').val();
            var stock = $('.stock').val();
            var bpid  = $('#file_id').val();
            var real_name = $('.real_name').val();
            var phone = $('.mobile').val();
            var email = $('.email').val();

            if(stock=='')stock = 0;
            if(money=='')money = 0;
            if(bpid=='')bpid = 0;

            $.ajax({
                type:'post',
                url: createProject,
                dataType: "json",
                data:{'invet':money,'sell_stock':stock,'file_bp_id':bpid,'phone':phone,'name':real_name,'email':email,'is_finance':is_finance},
                success:function(obj){
                    if(obj.level === "success"){
                        noty({text:obj.message,type:obj.level,
                            callback:{
                                afterClose:function(){
                                    window.location.href=Home;
                                }
                            }
                        });
                    }
                },
                error:function(obj){
                    var datajson = obj.responseJSON;
                    noty({text:datajson.message,type:datajson.level,timeout:600});
                }
            });
		}
	});
	
});